#include "SceneManager.h"
#include "ManualObjectFactory.h"
#include "SceneNode.h"
#include "EntityX.h"
#include "EntityMD2.h"
#include "ManualObject.h"
#include "Light.h"
//#include "LightNode.h"
#include "EntityXFactory.h"
#include "EntityMD2Factory.h"
//
namespace Euclid
{
	SceneManager::SceneManager()
	{

	}

	SceneManager::~SceneManager()
	{

	}

	EntityX* SceneManager::createEntityX( const std::string& EntityXName, const std::string& meshName )
	{
		//
		RenderableObject* o = EntityXFactory::getSingletonPtr()->createInstanceImpl(EntityXName, meshName);

		//
		return static_cast<EntityX*>(o);
	}


	EntityMD2* SceneManager::createEntityMD2( const std::string& EntityMD2Name, const std::string& md2Name )
	{
		//
		RenderableObject* o = EntityMD2Factory::getSingletonPtr()->createInstanceImpl(EntityMD2Name, md2Name);

		//
		return static_cast<EntityMD2*>(o);
	}


	Euclid::ManualObject* SceneManager::createManualObject( const std::string& name )
	{
		//
		RenderableObject* o = ManualObjectFactory::getSingletonPtr()->createInstanceImpl(name);

		//
		return static_cast<ManualObject*>(o);
	}

	SceneNode* SceneManager::getRootSceneNode()
	{
		SceneNodeMap::iterator it = _nodes.find(_RootSceneNode);
		if (_nodes.end() == it)
		{
			_nodes[_RootSceneNode] = new SceneNode(this, _RootSceneNode);
		}

		//
		return _nodes[_RootSceneNode];
	}

	SceneNode* SceneManager::createSceneNode(const std::string& name)
	{
		SceneNodeMap::iterator it = _nodes.find(name);
		if (_nodes.end() == it)
		{
			_nodes[name] = new SceneNode(this, name);
		}

		//
		return _nodes[name];
	}

	void SceneManager::render()
	{
		////
		//{
		//	LightNodeMap::iterator it = _lights.find(_RootLightNode);
		//	if (_lights.end() != it)
		//	{
		//		it->second->render();
		//	}
		//}

		//
		{
			SceneNodeMap::iterator it = _nodes.find(_RootSceneNode);
			if (_nodes.end() != it)
			{
				it->second->render();
			}
		}
	}

	Light* SceneManager::createLight( const std::string& name )
	{
		return new Light;
	}


	//LightNode* SceneManager::getRootLightNode()
	//{
	//	LightNodeMap::iterator it = _lights.find(_RootLightNode);
	//	if (_lights.end() == it)
	//	{
	//		_lights[_RootLightNode] = new LightNode(this, _RootLightNode);
	//	}

	//	//
	//	return _lights[_RootLightNode];
	//}

	//LightNode* SceneManager::createLightNode( const std::string& name )
	//{
	//	LightNodeMap::iterator it = _lights.find(name);
	//	if (_lights.end() == it)
	//	{
	//		_lights[name] = new LightNode(this, name);
	//	}

	//	//
	//	return _lights[name];
	//}

	SceneNode* SceneManager::getSceneNode( const std::string& name )
	{
		if (_nodes.find(name) == _nodes.end())
		{
			return NULL;
		}

		return _nodes[name];
	}

	//const std::string SceneManager::_RootLightNode("RootLightNode");

	const std::string SceneManager::_RootSceneNode("RootSceneNode");

}
